package com.lalosoft.gastostarjetaweb.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lalosoft.gastostarjetaweb.Constants;
import com.lalosoft.gastostarjetaweb.dao.EmailDao;
import com.lalosoft.gastostarjetaweb.model.Email;

@SuppressWarnings("serial")
public class RedirectAdminServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger("RedirectAdminServlet");

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String redirectTo = req.getParameter("to");
		String state = "";
		String toJsp = "";
		if(redirectTo.equals(Constants.REDIRECT_RECIBIDOS)){
			state = Constants.ESTADO_RECIBIDO;
			toJsp = "/inbox.jsp";
		} else {
			state = Constants.ESTADO_ENVIADO;
			toJsp = "/enviados.jsp";
		}
		log.log(Level.WARNING, "usuario admin");
		log.log(Level.WARNING, "Redirect to: " + state);
		List<Email> list = EmailDao.findByState(state);
		log.log(Level.WARNING, "Email list size: " + list.size());
		req.setAttribute("emails", list);
	    RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(toJsp);
	    requestDispatcher.forward(req, resp);
		
	}
	
}
